<h2>Why is this an issue?</h2>
<p>Disposing an object twice in the same method, either with the <a
href="https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/statements/using-statement">Using statement</a> or by calling
<code>Dispose</code> directly, is confusing and error-prone. For example, another developer might try to use an already-disposed object, or there can
be runtime errors for specific paths in the code.</p>
<p>In addition, even if the <a
href="https://learn.microsoft.com/en-us/dotnet/api/system.idisposable.dispose#System_IDisposable_Dispose">documentation</a> explicitly states that
calling the <code>Dispose</code> method multiple times should not throw an exception, some implementations still do it. Thus it is safer to not
dispose of an object twice when possible.</p>
<h2>How to fix it</h2>
<h3>Code examples</h3>
<h4>Noncompliant code example</h4>
<pre data-diff-id="1" data-diff-type="noncompliant">
Dim foo As New Disposable()
foo.Dispose()
foo.Dispose() ' Noncompliant
</pre>
<pre data-diff-id="2" data-diff-type="noncompliant">
Using bar As New Disposable()  ' Noncompliant
    bar.Dispose()
End Using
</pre>
<h4>Compliant solution</h4>
<pre data-diff-id="1" data-diff-type="compliant">
Dim foo As New Disposable()
foo.Dispose()
</pre>
<pre data-diff-id="2" data-diff-type="compliant">
Using bar As New Disposable()  ' Compliant

End Using
</pre>
<h2>Resources</h2>
<h3>Documentation</h3>
<ul>
  <li> <a href="https://learn.microsoft.com/en-us/dotnet/api/system.idisposable.dispose#System_IDisposable_Dispose">Dispose method</a> </li>
  <li> <a href="https://learn.microsoft.com/en-us/dotnet/api/system.iasyncdisposable.disposeasync">DisposeAsync method</a> </li>
  <li> <a href="https://learn.microsoft.com/en-us/dotnet/standard/garbage-collection/implementing-dispose">Implement a Dispose method</a> </li>
  <li> <a href="https://learn.microsoft.com/en-us/dotnet/standard/garbage-collection/implementing-disposeasync">Implement a DisposeAsync method</a>
  </li>
  <li> <a href="https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/statements/using-statement">Using statement</a> </li>
</ul>

